#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Coff.h"

Coff::Coff(const char *fname){
	if( strlen(fname) != NULL ){
		fp = fopen(fname, "rb+");
	}

	//init local variables
	header       = NULL;
	gSymIterator = NULL;
}

Coff::~Coff(){
	if( fp != NULL ){
		fclose(fp);
	}

	for( int i = 0; i < header->nSymbols; i++ ){
		delete symTable[i];
	}
	free(symTable);

	free(header);
}


void Coff::loadSymbols(){
	int i = 0;

	if( header == NULL ){
		header = (CoffHeader *)malloc(sizeof(CoffHeader));
		fread(header, sizeof(CoffHeader), 1, fp);
	}

	symTable = (Symbol**)malloc(sizeof(Symbol *) * header->nSymbols);
	for(; i < header->nSymbols; i++){
		symTable[i] = new Symbol( i, this );
	} 
}

SymIterator* Coff::SymIterator(){

	if( gSymIterator != NULL ){
		delete gSymIterator;
	}

	gSymIterator = new GlobalSymIterator( this->symTable, this->header->nSymbols );
	return gSymIterator;
}

